const fs = require("fs");
const path = require("path");

function getMockmodules() {
  return new Promise((resolve, reject) => {
    fs.readdir(path.resolve(__dirname, "./modules"), (err, files) => {
      if (err) {
        reject(err);
        return;
      }
      let modules = [];
      files.forEach((file) => {
        const module = require("./modules/" + file);
        modules = [...modules, ...module];
      });
      resolve(modules);
    });
  });
}

module.exports = async function SetupMock(app) {
  const modules = await getMockmodules();
  modules.forEach((module) => {
    app[module.method](module.url, (req, res) => {
      res.json(module.response);
    });
  });
};
